package com.sdh.onlinemarket.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdh.onlinemarket.common.annotation.AdminToken;
import com.sdh.onlinemarket.common.lang.Result;
import com.sdh.onlinemarket.common.util.DeleteFile;
import com.sdh.onlinemarket.entity.Contractitem;
import com.sdh.onlinemarket.service.ContractitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 施德昊
 * @since 2022-01-19
 */
@Api(tags = "合同方案相关模块")
@RestController
@RequestMapping("/contractitem")
public class ContractitemController {

    @Autowired
    ContractitemService contractitemService;

    @AdminToken
    @ApiOperation(value = "添加新的合同方案")
    @PostMapping("/setcitem")
    @ResponseBody
    public Result setitem(@RequestBody Contractitem contractitem){
        if(contractitem.getItemname()==null
                ||contractitem.getItemdescrip()==null
                ||contractitem.getProducearea()==null
                ||contractitem.getPrice()==null
//                ||contractitem.getClassic()==null
                ||contractitem.getProducetime()==null){
            return Result.fail("参数错误");
        }
        System.out.println(contractitem);
        System.out.println("---------------------------------"+ LocalDateTime.now());
        contractitem.setIstatus(0);
        if(contractitemService.save(contractitem)){
            System.out.println("请求添加商品了！"+LocalDateTime.now());
            return Result.succ("成功！");
        }else {
            System.out.println("请求添加商品失败！"+LocalDateTime.now());
            return Result.fail("输入有误！");
        }
    }

    @AdminToken
    @ApiOperation(value = "修改合同方案")
    @PostMapping("/modifycitem")
    @ResponseBody
    public Result modifyitem(@RequestBody Contractitem contractitem){
        if(contractitem.getItemname()==null
                ||contractitem.getItemdescrip()==null
                ||contractitem.getProducearea()==null
                ||contractitem.getPrice()==null
//                ||contractitem.getClassic()==null
                ||contractitem.getProducetime()==null){
            return Result.fail("参数错误");
        }
        contractitem.setIstatus(0);
        if(contractitemService.update(contractitem,new QueryWrapper<Contractitem>().eq("id",contractitem.getId()))){
            System.out.println("成功修改商品了！"+LocalDateTime.now());
            return Result.succ("成功！");
        }else {
            System.out.println("修改商品失败！"+LocalDateTime.now());
            return Result.fail("输入有误！");
        }
    }

    @ApiOperation(value = "获取方案列表")
    @PostMapping("/getitemlist")
    @ResponseBody
    public Result getItemList(){
        return Result.succ("success",contractitemService.list(new QueryWrapper<Contractitem>().orderByDesc("producetime")));
    }


    @ApiOperation(value = "获取类别列表",notes = "这个类别可以作为普通商品的子类这一层，大类就写陈皮方案")
    @PostMapping("/gettype")
    @ResponseBody
    public Result getItembytype1(String classic){

//        List<Item> itemList = itemService.list(new QueryWrapper<Item>()
//                .select("distinct subclassic ,classic ")
//                .groupBy("subclassic")
//                .eq("classic",classic));

//                List<Item> itemList = itemService.list(new QueryWrapper<Item>()
//                .eq("classic",classic));

        List<Contractitem> contractitemList = contractitemService.list(new QueryWrapper<Contractitem>().groupBy("classic"));
        System.out.println(contractitemList);
        if(contractitemList == null||contractitemList.size() == 0){
            return Result.fail("item not found");
        }
        return Result.succ("success",contractitemList);
    }


    @ApiOperation(value = "根据类别获取方案列表")
    @PostMapping("/getitembytype")
    @ResponseBody
    public Result getItembytype2(String classic){
        List<Contractitem> contractitemList = contractitemService.list(new QueryWrapper<Contractitem>().eq("classic",classic));
        if(contractitemList == null||contractitemList.size() == 0){
            return Result.fail("item not found");
        }
        return Result.succ("success",contractitemList);
    }

    @AdminToken
    @ApiOperation(value = "获取一个方案的详细信息")
    @PostMapping("/getsingalcitem")
    @ResponseBody
    public Result getSingalItem(String id){
        Contractitem contractitem = contractitemService.getOne(new QueryWrapper<Contractitem>().eq("id",id));
        if(contractitem == null){
            return Result.fail("item not found");
        }
        return Result.succ("success",contractitem);
    }

    @AdminToken
    @ApiOperation(value = "删除一个方案")
    @PostMapping("/citemdel")
    @ResponseBody
    public Result DelItem(String id){
        Contractitem contractitem = contractitemService.getOne(new QueryWrapper<Contractitem>().eq("id",id));
        if(contractitemService.remove(new QueryWrapper<Contractitem>().eq("id",id))){
            if(contractitem.getIavatar() != null){
                DeleteFile.deletefileB(contractitem.getIavatar());
            }
            return Result.succ("remove success");
        }else {
            return Result.fail("item not found");
        }
    }


    @ApiOperation(value = "关键词搜索方案")
    @PostMapping("/citemsearch")
    @ResponseBody
    public Result SearchItem(String itemname){
        List<Contractitem> contractitemList = contractitemService.list();
        String keywords[];
        if(StringUtils.indexOf(itemname," ")>0){
            keywords = itemname.split(" ");
        }else {
            keywords = new String[]{itemname};
        }
        List<Contractitem> contractItemReturnList = new ArrayList<>();
        for(Contractitem contractitemT:contractitemList){
            boolean isExist = true;
            for(String words:keywords){
                if (StringUtils.isBlank(words)){
                    continue;
                }
                if(StringUtils.indexOf(contractitemT.getItemname(),words) == -1){
                    isExist = false;
                }
                System.out.println(contractitemT.getItemname()+"  "+words+"  "+isExist);
            }
            if (isExist){
                contractItemReturnList.add(contractitemT);
            }
        }

        if(contractItemReturnList.size()>0){
            return Result.succ("this is itemlist",contractItemReturnList);
        }else {
            return Result.fail("no item exist");
        }
    }

}
